package name.zxw.rabbitmq.exchange3;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmCallback;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;

/**
 * @author zhangxinwang
 * @date 2022/6/22 16:44
 */
public class MyPublisher {

    public static void main(String[] args) throws Exception {
        String exchangeName = "my_topic";

        ConnectionFactory factory = new ConnectionFactory();
        Channel channel = factory.newConnection().createChannel();

        ConfirmCallback ackCallback = (deliveryTag, multiple) -> {
            System.out.println("确认回调:");
            System.out.println("deliveryTag = " + deliveryTag);
            System.out.println("multiple = " + multiple);
        };
        ConfirmCallback nackCallback = (deliveryTag, multiple) -> {
            System.out.println("确认失败回调:");
            System.out.println("deliveryTag = " + deliveryTag);
            System.out.println("multiple = " + multiple);
        };

        channel.addConfirmListener(ackCallback, nackCallback);

        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC);


        channel.basicPublish(exchangeName,"quick.orange.rabbit",null,"quick.orange.rabbit".getBytes(StandardCharsets.UTF_8));
        channel.basicPublish(exchangeName,"lazy.orange.elephant",null,"lazy.orange.elephant".getBytes(StandardCharsets.UTF_8));
        channel.basicPublish(exchangeName,"quick.orange.fox",null,"quick.orange.fox".getBytes(StandardCharsets.UTF_8));
        channel.basicPublish(exchangeName,"lazy.brown.fox",null,"lazy.brown.fox".getBytes(StandardCharsets.UTF_8));
        channel.basicPublish(exchangeName,"lazy.pink.rabbit",null,"lazy.pink.rabbit".getBytes(StandardCharsets.UTF_8));
        channel.basicPublish(exchangeName,"quick.brown.fox",null,"quick.brown.fox".getBytes(StandardCharsets.UTF_8));
        channel.basicPublish(exchangeName,"quick.orange.male.rabbit",null,"quick.orange.male.rabbit".getBytes(StandardCharsets.UTF_8));
        channel.basicPublish(exchangeName,"lazy.orange.male.rabbit",null,"lazy.orange.male.rabbit".getBytes(StandardCharsets.UTF_8));

    }


}
